<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? 'Add新增' : 'Update修改'" width="60%" :close-on-click-modal="false" >
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="140px" @keyup.enter="submitHandle()">
			<el-row>
				<el-col :span="12">
					<el-form-item prop="followUpDate" label="FollowUpDate 跟进日期" label-width="160px">
						<el-date-picker
							v-model="dataForm.followUpDate"
							type="date"
							placeholder="Choose Date选择日期"
							:shortcuts="store.commonStore.shortcuts"
							value-format="YYYY-MM-DD HH:mm:ss"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="Follower 跟进人">
						<el-input v-model="dataForm.followerName" disabled></el-input>
						
						<!-- <el-select v-model="dataForm.followerId" remote remote-method="getCustomerList"
						filterable clearable placeholder="please input a keyword">
							<el-option
							v-for="item in csOptions"
							:key="item.id"
							:label="item.realName"
							:value="item.id"
							/>
						</el-select> -->
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item prop="method" label="Method 跟进方式">
						<el-input v-model="dataForm.method" placeholder=""></el-input>
					</el-form-item>
			<el-row>
				<el-col :span="12">
					<el-form-item prop="mobile" label="Mobile 客户电话" label-width="150px">
						<el-input v-model="dataForm.mobile" disabled></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item v-show="props.type !== 'client'" prop="customerName" label="Client 跟进客户">
						<el-select v-model="dataForm.customerId" remote :remote-method="getClientList" 
						filterable clearable placeholder="please input a keyword" @change="customerChange">
							<el-option
							v-for="item in clientOptions"
							:key="item.id"
							:label="item.name"
							:value="item.id"
							/>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			
			<el-form-item prop="progress" label="Progress 跟进情况">
				<el-input v-model="dataForm.progress" placeholder="" type="textarea"></el-input>
			</el-form-item>
			<el-form-item prop="remark" label="Remark 备注">
				<el-input v-model="dataForm.remark" placeholder="" type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">Cancel 取消</el-button>
			<el-button type="primary" @click="submitHandle()">Confirm 确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import store from '@/store'
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import {  useFollowApi, useFollowSubmitApi } from '@/api/follow'
import {  selectCustomerList, getClientListApi } from '@/api/client'
import { queryUserInfo } from '@/api/user'

const emit = defineEmits(['refreshDataList'])
const props = defineProps({
	type: {
		type: String,

	},
	clientId: {
		type: String,
		required: true
	},
	clientName: {
		type: String,
		required: true
	},
	mobile: {
		type: String,
		required: true
	},
})

const visible = ref(false)
const dataFormRef = ref()
const dataForm = reactive({
	id: '',
	customerId: '',
	customerName: '',
	method: '',
	progress: '',
	remark: '',
	followUpDate: '',
	mobile: '',
	followerId: '',
	followerName: ''
})

// 客户选项下拉
const clientOptions = ref([])
const getClientList = (query: string) => {
	let req = {
		page: 1,
		limit: 100,
		customerName: query,
		order: 'CREATE_TIME'
	}
	// selectCustomerList(req).then(res => {
	// 	clientOptions.value = res.data.list;
	// })
	getClientListApi(req).then(res => {
		clientOptions.value = res.data.list;
	})
}

// 客服选项下拉
const  csOptions = ref([]);
const getUsersList = (type: string) => {
	let req = {
		page: 1,
		limit: 100,
		roleName: type
	}
	queryUserInfo(req).then(res => {
		switch(type){
			// case 'SALES': salesOptions.value = res.data.list; break;
			case 'CS': csOptions.value = res.data.list; break;
		}
	})
}
getUsersList('CS');

if(props.type === 'client'){
	dataForm.mobile = props.mobile;
	dataForm.customerName = props.clientName;
}else{
	getClientList('');
}

const dataRules = ref({
	name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	// mobile: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''
	dataForm.followerId = store.userStore.user.id;
	dataForm.followerName = store.userStore.user.username;

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if(props.clientId && props.clientId !== ''){
		dataForm.customerId = props.clientId;
		dataForm.customerName = props.clientName;
	}

	// id 存在则为修改
	if (id) {
		getFollowInfo(id)
	}
}

const getFollowInfo = (id: number) => {
	useFollowApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const customerChange = (val) => {
	let arr = clientOptions.value as any;
	for(let item of arr){
		if(item.id === val){
			dataForm.mobile = item.mobile;
		}
	}
}

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
		useFollowSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
